package org.jeecg.modules.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.entity.BusMemberCard;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.mall.entity.MallOrderGoods;
import org.jeecg.modules.mall.entity.MallOrderGoodsDetail;
import org.jeecg.modules.mall.service.IMallOrderGoodsDetailService;
import org.jeecg.modules.mall.service.IMallOrderGoodsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: mall_order_goods
 * @Author: jeecg-boot
 * @Date:   2023-05-06
 * @Version: V1.0
 */
@Api(tags="mall_order_goods")
@RestController
@RequestMapping("/mall/mallOrderGoods")
@Slf4j
public class MallOrderGoodsController extends JeecgController<MallOrderGoods, IMallOrderGoodsService> {
	@Autowired
	private IMallOrderGoodsService mallOrderGoodsService;
	 @Autowired
	 private IBusHotelService busHotelService;
	 @Resource
	 private IMallOrderGoodsDetailService mallOrderGoodsDetailService;
	 @Resource
	 private CesGoodsServiceImpl cesGoodsService;

	/**
	 * 分页列表查询
	 *
	 * @param mallOrderGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "mall_order_goods-分页列表查询")
	@ApiOperation(value="mall_order_goods-分页列表查询", notes="mall_order_goods-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MallOrderGoods>> queryPageList(MallOrderGoods mallOrderGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   @RequestParam(name="startTime",required = false) String startTime,
													   @RequestParam(name="endTime",required = false) String endTime,
													   @RequestParam(name="hotelIds[]",required = false) List<String> hotelIds,
								   HttpServletRequest req) {
		LambdaQueryWrapper<MallOrderGoods> queryWrapper = QueryGenerator.initQueryWrapper(mallOrderGoods, req.getParameterMap()).lambda();
		if (ObjectUtils.isNotEmpty(startTime)) {
			queryWrapper.ge(MallOrderGoods::getCreateTime, DateUtil.parseDate(startTime));
		}
		if (ObjectUtils.isNotEmpty(endTime)) {
			queryWrapper.le(MallOrderGoods::getCreateTime, DateUtil.parseDate(endTime));
		}
		if (ObjectUtils.isNotEmpty(hotelIds)) {
			queryWrapper.and(j -> {
				for (int k = 0; k < hotelIds.size(); k++) {
					j = j.or().eq(MallOrderGoods::getHotelId, hotelIds.get(k));
				}
			});
		}
		Page<MallOrderGoods> page = new Page<MallOrderGoods>(pageNo, pageSize);
		IPage<MallOrderGoods> pageList = mallOrderGoodsService.page(page, queryWrapper);
		pageList.getRecords().forEach(t -> {
			if (t.getStatus().equals(1)) {
				t.setStatusName("待发货");
			} else if (t.getStatus().equals(2)) {
				t.setStatusName("待收货");
			} else if (t.getStatus().equals(3)) {
				t.setStatusName("已完成");
			} else if (t.getStatus().equals(4)) {
				t.setStatusName("已拒绝");
			} else if (t.getStatus().equals(5)) {
				t.setStatusName("已取消");
			}
			BusHotel hotel = busHotelService.getById(t.getHotelId());
			if (hotel != null) {
				t.setHotelName(hotel.getName());
			}
			LambdaQueryWrapper<MallOrderGoodsDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(MallOrderGoodsDetail::getOrderCode, t.getCode());
			List<MallOrderGoodsDetail> list=mallOrderGoodsDetailService.list(lambdaQueryWrapper);
			if(ObjectUtils.isNotEmpty(list)){
				list.forEach(i->{
					i.setGoodsInfo(cesGoodsService.getById(i.getGoodsId()));
				});
			}
			t.setDetailList(list);
		});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param mallOrderGoods
	 * @return
	 */
	@AutoLog(value = "mall_order_goods-添加")
	@ApiOperation(value="mall_order_goods-添加", notes="mall_order_goods-添加")
	//@RequiresPermissions("mall:mall_order_goods:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MallOrderGoods mallOrderGoods) {
		mallOrderGoodsService.save(mallOrderGoods);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mallOrderGoods
	 * @return
	 */
	@AutoLog(value = "mall_order_goods-编辑")
	@ApiOperation(value="mall_order_goods-编辑", notes="mall_order_goods-编辑")
	//@RequiresPermissions("mall:mall_order_goods:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MallOrderGoods mallOrderGoods) {
		mallOrderGoodsService.updateById(mallOrderGoods);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "mall_order_goods-通过id删除")
	@ApiOperation(value="mall_order_goods-通过id删除", notes="mall_order_goods-通过id删除")
	//@RequiresPermissions("mall:mall_order_goods:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		mallOrderGoodsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "mall_order_goods-批量删除")
	@ApiOperation(value="mall_order_goods-批量删除", notes="mall_order_goods-批量删除")
	//@RequiresPermissions("mall:mall_order_goods:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mallOrderGoodsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "mall_order_goods-通过id查询")
	@ApiOperation(value="mall_order_goods-通过id查询", notes="mall_order_goods-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<MallOrderGoods> queryById(@RequestParam(name="id",required=true) String id) {
		MallOrderGoods mallOrderGoods = mallOrderGoodsService.getById(id);
		if(mallOrderGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mallOrderGoods);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mallOrderGoods
    */
    //@RequiresPermissions("mall:mall_order_goods:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MallOrderGoods mallOrderGoods) {
        return super.exportXls(request, mallOrderGoods, MallOrderGoods.class, "mall_order_goods");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("mall:mall_order_goods:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MallOrderGoods.class);
    }

}
